package com.example.drools.service.impl;

import com.example.drools.entity.PointDomain;
import com.example.drools.service.PointRuleEngine;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.drools.core.impl.InternalKnowledgeBase;
import org.drools.core.impl.KnowledgeBaseFactory;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderError;
import org.kie.internal.builder.KnowledgeBuilderErrors;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;

/**
 * @author ddsyw
 * @version 1.0
 * @date 2021/12/21 2:44 下午
 */
@Slf4j
public class PointRuleEngineImpl implements PointRuleEngine {

    private InternalKnowledgeBase internalKnowledgeBase = null;

    @Override
    public void executeRuleEngine(PointDomain pointDomain) {
        if (Objects.isNull(internalKnowledgeBase) || Objects.isNull(pointDomain)) {
            log.error("internalKnowledgeBase is null");
            return;
        }
        KieSession ksession = internalKnowledgeBase.newKieSession();
        ksession.insert(pointDomain);
        //自定义实现 执行
//    AgendaFilter agendaFilter;
        ksession.fireAllRules();
    }

    @Override
    public void initEngine() {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("back/addpoint.drl"), ResourceType.DRL);
        kbuilder.add(ResourceFactory.newClassPathResource("back/subpoint.drl"), ResourceType.DRL);
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error : errors) {
                log.error("know error:{}", error);
            }
            throw new IllegalArgumentException("Could not parse knowledge.");
        }
        internalKnowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
        internalKnowledgeBase.addPackages(kbuilder.getKnowledgePackages());
    }

    @Override
    public void refreshEnginRule() {
        internalKnowledgeBase.removeKiePackage("com.drools.demo.point");
    }

    public static void main(String... args) {
        PointRuleEngineImpl pointRuleEngine = new PointRuleEngineImpl();
        pointRuleEngine.initEngine();
        PointDomain pointDomain = new PointDomain();
        pointDomain.setUserName("法外狂徒张三");
        pointDomain.setBackMondy(100d);
        pointDomain.setBuyMoney(500d);
        pointDomain.setBackNums(1);
        pointDomain.setBuyNums(5);
        pointDomain.setBillThisMonth(5);
        pointDomain.setBirthDay(true);
        pointDomain.setPoint(0);
        pointRuleEngine.executeRuleEngine(pointDomain);
        log.info("执行完毕BillThisMonth：" + pointDomain.getBillThisMonth());
        log.info("执行完毕BuyMoney：" + pointDomain.getBuyMoney());
        log.info("执行完毕BuyNums:" + pointDomain.getBuyNums());
        log.info("执行完毕规则引擎决定发送积分：" + pointDomain.getPoint());
    }
}
